package ru.aslanov.schedule.client.ds;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.FieldValueExtractor;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.types.FieldType;
import ru.aslanov.schedule.client.ScheduleUser;
import ru.aslanov.schedule.shared.DayOfWeek;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * Created: Jan 29, 2010 7:15:23 PM
 *
 * @author Sergey Aslanov
 */
public class GroupsDS extends ChildDS {
    private static Map<String, DataSource> instances = new HashMap<String, DataSource>();

    public static DataSource getInstance(ScheduleUser scheduleUser) {
        DataSource dataSource = instances.get(scheduleUser.getScheduleKey());
        if (dataSource == null) {
            dataSource = new GroupsDS(scheduleUser.getScheduleKey(), scheduleUser);
            instances.put(scheduleUser.getScheduleKey(), dataSource);
        }
        return dataSource;
    }

    private GroupsDS(String scheduleKey, InputLangProvider inputLangProvider) {
        super("Group", scheduleKey, "schedule", inputLangProvider);

        DataSourceField dance = new DataSourceField("danceKey", FieldType.TEXT, cnt.danceField());
        //dance.setForeignKey("Dance" + scheduleKey + ".encodedKey");
        dance.setRequired(false);

        DataSourceField level = new DataSourceField("levelKey", FieldType.TEXT, cnt.levelField());
        //level.setForeignKey("Level" + scheduleKey + ".encodedKey");
        level.setRequired(false);

        DataSourceField teacher1 = new DataSourceField("teacher1Key", FieldType.TEXT, cnt.teacherField() + " 1");
        //teacher1.setForeignKey("Teacher" + scheduleKey + ".encodedKey");
        teacher1.setRequired(false);

        DataSourceField teacher2 = new DataSourceField("teacher2Key", FieldType.TEXT, cnt.teacherField() + " 2");
        //teacher2.setForeignKey("Teacher" + scheduleKey + ".encodedKey");
        teacher2.setRequired(false);
        teacher2.setCanFilter(false);

        DataSourceField location = new DataSourceField("locationKey", FieldType.TEXT, cnt.locationField());
        //location.setForeignKey("Location" + scheduleKey + ".encodedKey");
        location.setRequired(false);

        DataSourceField gCalendar = new DataSourceField("calendarKey", FieldType.TEXT, cnt.gcalendar());

        DataSourceDateField started = new DataSourceDateField("started", cnt.startedField());
        started.setUseTextField(true);
        started.setCanFilter(false);
        started.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);

        DataSourceDateField until = new DataSourceDateField("until", cnt.untilField());
        until.setUseTextField(true);
        until.setCanFilter(false);
        until.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);

        DataSourceField welcome = new DataSourceField("welcome", FieldType.TEXT, cnt.welcomeField());
        welcome.setCanFilter(false);

        DataSourceField comment = new DataSourceField("comment", FieldType.TEXT, cnt.commentField(), 2000);
        //comment.setEditorType(new TextAreaItem());
        comment.setCanFilter(false);

        DataSourceField privates = new DataSourceField("privates", FieldType.BOOLEAN, cnt.privatesField());

        DataSourceField hidden = new DataSourceField("hidden", FieldType.BOOLEAN, cnt.hiddenField());

        DataSourceField title = new DataSourceField("title", FieldType.TEXT, cnt.nameField());
        title.setCanFilter(false);

        DataSourceField days = new DataSourceField("days", FieldType.TEXT, cnt.daysField());
        days.setCanEdit(false);
        days.setCanSave(false);
        days.setCanFilter(false);
        days.setFieldValueExtractor(new FieldValueExtractor() {
            @Override
            public Object execute(Object o, Object val, DataSourceField dataSourceField, String s) {
                if (val != null && val.toString().length() > 0) {
                    StringBuilder sb = new StringBuilder();
                    final String[] strings = val.toString().split(",");
                    for (String string : strings) {
                        try {
                            final DayOfWeek dayOfWeek = DayOfWeek.valueOf(string);
                            String localized = cnt.daysOfWeekShort()[dayOfWeek.ordinal()];
                            if (sb.length() > 0) sb.append(", ");
                            sb.append(localized);
                        } catch (IllegalArgumentException e) {
                            // ignore
                        }
                    }
                    return sb.toString();
                } else {
                    return null;
                }
            }
        });

        setFields(encodedKey, /*scheduleKey, */title, privates, dance, /*danceName,*/ level, /*levelName,*/
                teacher1, /*teacher1Name,*/ teacher2, /*teacher2Name,*/ location, /*locationName,*/ days,
                started, until, welcome, comment, gCalendar, hidden);
    }

}
